package com.example.springboot_jap_model.handle;

import com.example.springboot_jap_model.domain.RespEntity;
import com.example.springboot_jap_model.enums.RespEntityEnum;
import com.example.springboot_jap_model.exception.AutoException;
import com.example.springboot_jap_model.utils.RespEntityUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一 异常处理
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public RespEntity handel(Exception e){
        if (e instanceof AutoException) {
            AutoException autoException = (AutoException) e;
            return RespEntityUtils.error(autoException.getCode(),autoException.getMessage());
        } else {
            logger.error("错误信息:{}",e);
            return RespEntityUtils.error(RespEntityEnum.UN_KNOWS);
        }
    }

    /**
     * 权限 不符合要求 时刻 抛出异常
     * 1.并跳转到 错误页面
     * @param auto
     * @return
     */
    @ExceptionHandler(value = AutoException.class)
    public String autoHandel(AutoException auto){
        logger.error("错误提示信息:{},{}",auto.getCode(),auto.getMessage());
        logger.error("错误信息:{}",auto);
        return "/403";
    }

}
